/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.TimeClockServer.messages.tx;

import TimeRecording.TimeClockServer.dtos.DatabaseTransaction;
import TimeRecording.TimeClockServer.messages.ClockMessage;
import java.util.Formatter;

/**
 *
 * @author david
 */
public class PositiveResponseMessage extends ClockMessage {

    private final static String MSG_FORMAT =
            "[HDR]PR10[RS]" +
                "%1$5s[RS]" +        // Prompt number (5 chars)
                "%2$s[RS]" +         // Free format message
                "%3$04d[RS]" +       // Message timeout (4 digits)
                "1[RS]";             // Good Beep (boolean - 0 | 1)

    private final static String USER_MSG_FORMAT = "%1$-20s%2$-20s";

    public PositiveResponseMessage(DatabaseTransaction transaction) {
        
        super(new Formatter().format(MSG_FORMAT,
                transaction.getPrompt(),
                getMessage(transaction),
                getMessageTimeout(transaction)).toString());
    }

    private static String getMessage(DatabaseTransaction transaction) {

        String line1;
        String line2;
        
        if (transaction.getBadgeDetails() == null) {
            line1 = "Badge " + transaction.getBadgeNo();
            line2 = "Not Recognised";
            
        } else if (transaction.getBadgeDetails().getEmployeeId() == 0) {
            line1 = "Badge " + transaction.getBadgeNo();
            line2 = "Not Associated";
        
        } else {
            line1 = transaction.getBadgeDetails().getEmployeeName();
            line2 = transaction.getClockedInOrOut().toString();
        }
        
        return new Formatter().format(USER_MSG_FORMAT, line1, line2).toString();
    }

    private static int getMessageTimeout(DatabaseTransaction transaction) {

        if (transaction.getBadgeDetails() == null) {
            return 9999;
        } else if (transaction.getBadgeDetails().getEmployeeId() == 0) {
            return 9999;
        } else {
            return 5000;
        }
    }
}
